package com.changgou.order.mq.listener;

import com.alibaba.fastjson.JSON;
import com.changgou.order.service.OrderService;
import com.github.wxpay.sdk.WXPayUtil;
import entity.HttpClient;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

/**
 * rabbitmq监听对于的队列
 */
@Component
@RabbitListener(queues = "${mq.pay.queue.order}")
public class OrderMessageListener {

    @Autowired
    private OrderService orderService;

    @Value("${weixin.appid}")
    private String appid;  //应用id
    @Value("${weixin.partner}")
    private String partner;  //商户id
    @Value("${weixin.partnerkey}")
    private String partnerkey;   //密钥

    /**
     * 支付结果监听
     */
    @RabbitHandler
    public void getMessage(String message) throws Exception {
        //支付结果
        Map<String,String> resultMap = JSON.parseObject(message, Map.class);
        //通信标识 return_code
        String return_code = resultMap.get("return_code");
        //如果通信成功才有接下来的事情
        if (return_code.equals("SUCCESS")){
            //业务结果 result_code
            String result_code = resultMap.get("result_code");

            //订单号out_trade_no
            String out_trade_no = resultMap.get("out_trade_no");
            //支付成功修改订单状态
            if (result_code.equals("SUCCESS")){
                //微信支付交易流水号transaction_id
                orderService.updateStatus(resultMap.get("transaction_id"),resultMap.get("time_end"),resultMap.get("transaction_id"));
            }else {
                //支付失败，关闭支付，回滚库存
                //这里删之前要先关闭订单， 告诉微信服务器，关闭订单，不让付款了  在微信开发那看关闭
                //1,关闭订单
                Map<String,String> wxMap = new HashMap<>();
                wxMap.put("appid",appid);  //公众号id、
                wxMap.put("mch_id",partner);  //商户号
                wxMap.put("out_trade_no",out_trade_no); //订单号
                String nonceStr = WXPayUtil.generateNonceStr(); //随机字符串
                wxMap.put("nonce_str",nonceStr);
                String signedXml = WXPayUtil.generateSignedXml(wxMap, partnerkey); //转为xml自动生成签名
                String url = "https://api.mch.weixin.qq.com/pay/closeorder";  ////URL地址   腾讯给的地址，复制过来即可
                HttpClient httpClient = new HttpClient(url);   //利用HTTPclient 发接数据
                httpClient.setHttps(true);  //1,提交方式
                httpClient.setXmlParam(signedXml);   //2,提交参数
                httpClient.post();  //3,执行请求
                //4，接收返回数据 并且转化为map。
                String content = httpClient.getContent();
                Map<String, String> xmlToMap = WXPayUtil.xmlToMap(content);
                //这里看需要是否进一步对map返回结果进行处理， 比如关闭失败信息等等
                orderService.deleteOrder(resultMap.get("transaction_id"));
            }

        }
    }
}
